<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :before-close="closeDlg"
    :close-on-click-modal="false"
    destroy-on-close="true"
    :width="width"
  >
    <div :style="'width: 100%; height: ' + height + ';'" class="scrollbar">
      <el-scrollbar style="height: 100%;">
        <!--    表单-->
        <el-form
          ref="dataForm"
          :model="inputList"
          label-position="right"
          label-width="100px"
          style="margin-left:0px;"
        >
          <template v-for="(item, index) in inputList">
            <el-form-item
              v-show="item.show"
              v-if="item.type==='text'"
              :key="index"
              :label="item.title"
              :prop="item.name"
            >
              <el-input
                :ref="item.name"
                v-model="item.value"
                :placeholder="item.title"
                :style="'width: ' + item.width + ';'"
                @change="common_event(item.change, index)"
              />
            </el-form-item>
            <el-form-item
              v-show="item.show"
              v-if="item.type==='freq'"
              :key="index"
              :label="item.title"
              :prop="item.name"
            >
              <el-input-freq
                :ref="item.name"
                v-model="item.value"
                :placeholder="item.title"
                :style="'width: ' + item.width + ';'"
                :isPositive="true"
              />
            </el-form-item>
          </template>
        </el-form>
      </el-scrollbar>
    </div>

    <!--    对话框下方按钮-->
    <div slot="footer" class="dialog-footer">
      <!--    取消按钮-->
      <el-button @click="closeDlg">
        {{ $t('table.cancel') }}
      </el-button>

      <!--    提交按钮-->
      <el-button type="primary" :loading="loadingBtn" @click="createData">
        {{ loadingBtnTitle }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { controlAmount, isNotEmpty, zEnBase64 } from '@/utils'
import ElInputFreq from '@/common/weixing/components/ElInputFreq/index'
import { getItem, getToken } from '@/utils/auth'
import global from '@/common'
import { batchCreateChainFrequencyData } from '@/api/weixing/chainFrequencyData'
import { getChainFrequencyDataList } from '@/api/weixing/base'

export default {
  name: 'ChainFrequencyData',
  components: { ElInputFreq },
  props: {
    width: {
      type: String,
      default: '500px'
    },
    height: {
      type: String,
      default: '280px'
    },
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '批量创建数据'
    },
    row: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loadingBtn: false, // 防止表单重复提交
      loadingBtnTitle: '确定', // 提交按钮标题
      inputList: [],
      rules: []
    }
  },
  closeDlg() {
    this.visible = false
    this.$parent.batchDataVisible = false
  },
  created() {
    this.inputList = [
      { show: true, name: 'beginFrequency', title: '起始频率', type: 'freq', width: '330px', value: '', change: 'makeFrequency' },
      { show: true, name: 'endFrequency', title: '终止频率', type: 'freq', width: '330px', value: '', change: 'makeFrequency' },
      // { show: true, name: 'centerFrequency', title: '中心频率', type: 'freq', width: '330px', value: '', change: 'makeFrequency' },
      { show: true, name: 'intervalFrequency', title: '频率间隔', type: 'freq', width: '330px', value: '', change: 'makeFrequency' },
      { show: true, name: 'coupling', title: '默认耦合', type: 'text', width: '330px', value: '', change: null },
      { show: true, name: 'loss', title: '默认损耗', type: 'text', width: '330px', value: '', change: null },
      { show: true, name: 'gain', title: '默认增益', type: 'text', width: '330px', value: '', change: null }
    ]
    this.rules = {
      beginFrequency: [{ required: true, message: '起始频率不能为空', trigger: 'change' }]
    }
  },
  methods: {
    createData() {
      this.$refs['dataForm'].validate(async valid => {
        if (valid) {
          this.loadingBtn = true // 提交按钮不可用
          this.loadingBtnTitle = '创建中...'
          const data = { token: getToken(), functionID: global.functionID }
          let params = ''
          this.inputList.map(ele => {
            if (isNotEmpty(ele.value)) {
              // params += ele.name + '=' + zEnBase64(ele.value)
              // params += (isNotEmpty(params) && params.substring(0, 1) !== ',' ? ',' : '')
              // data.push(JSON.parse('{"' + ele.name + '":"' + ele.value + '"}'))
              eval('data.' + ele.name + '="' + ele.value + '"')
            }
          })
          if (isNotEmpty(this.row.ID)) {
            params += 'chainFrequencyDefineID=' + zEnBase64(this.row.ID)
            data.chainFrequencyDefineID = this.row.ID
          }
          if (isNotEmpty(this.row.applicationID)) { params += ',applicationID=' + zEnBase64(this.row.applicationID) }
          if (isNotEmpty(this.row.organizationID)) { params += ',organizationID=' + zEnBase64(this.row.organizationID) }
          if (isNotEmpty(this.row.stationID)) {
            params += ',stationID=' + zEnBase64(this.row.stationID)
            data.stationID = this.row.stationID
          }
          if (isNotEmpty(this.row.chainID)) {
            params += ',chainID=' + zEnBase64(this.row.chainID)
            data.chainID = this.row.chainID
          }
          params = params.substring(0, params.length - 1)
          data.parameterArray = params
          console.log(data)
          const { body, header } = await batchCreateChainFrequencyData(data)
          if (header.code === 0) {
            this.$notify({
              title: '创建成功',
              message: '数量：' + body.ids.length,
              type: 'success',
              duration: global.notify.durationShort
            })
            this.closeDlg()
          } else {
            this.loadingBtn = false // 提交按钮可用
            this.loadingBtnTitle = '确定'
            this.$notify({
              title: '创建失败',
              message: header.msg,
              type: 'error',
              duration: global.notify.duration
            })
          }
        }
      })
    },
    closeDlg() {
      this.visible = false
      this.$parent.batchDataVisible = false
    },
    common_event(event_name, index) {
      if (isNotEmpty(event_name)) {
        this[event_name](index)
      }
    },
    makeFrequency(index) {
      this.$nextTick(() => {
        this.inputList[index].value = controlAmount(this.inputList[index].value)
      })
    }
  }
}
</script>

<style>
  .el-scrollbar__wrap {
    overflow-x: hidden;
  }
</style>
